<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
     function fn(arr) {
      const n = arr.length;
      arr.sort((a, b) => a - b);
      arr[0] = 1;
      for (let i = 1; i < n; ++i) {
        arr[i] = Math.min(arr[i], arr[i - 1] + 1);
      }
      return arr[n - 1];
    }
    console.log(fn([2, 2, 1, 2, 1]))
  </script>
</body>
</html>